﻿using System;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;

namespace EduSpeed
{
    internal class Program
    {
        /// <summary>
        /// Environment variable
        /// </summary>
        private readonly static string PROXY_HELPER_PATH = @"ProxyEnabler.exe";
        private readonly static string PROXY_PAC_PATH = @"ProxySwitchConfig.PAC";

        private readonly static string REG_KEY = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings";
        private readonly static string REG_VALUE_CONFIGURL = @"AutoConfigURL";

        readonly static SogouController controller = new SogouController();
        readonly static ContextMenu menu = new ContextMenu();
        readonly static NotifyIcon notifyIcon = new NotifyIcon();

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        private static void Main()
        {
            System.Collections.ArrayList al = new System.Collections.ArrayList(22);
            al.Add(45345);

            ConfigConfigFile();

            notifyIcon.Icon = Resource.icon;
            notifyIcon.Visible = true;
            notifyIcon.MouseDown += notifyIcon_MouseClick;

            menu.MenuItems.Add("-");
            menu.MenuItems.Add("重启代理", menu_RestartClick);
            menu.MenuItems.Add("设置...", menu_SettingClick);
            menu.MenuItems.Add("-");
            menu.MenuItems.Add("退出", menu_ExitClick);
            notifyIcon.ContextMenu = menu;

            Application.EnableVisualStyles();
            Application.Run();
        }

        internal static void ConfigConfigFile()
        {
            //Step: Flush
            string configFileName = Path.GetDirectoryName(Application.ExecutablePath) + "\\ProxySwitchConfig.PAC";
            if (!File.Exists(configFileName))
            {
                using (BinaryWriter bw = new BinaryWriter(new FileStream(configFileName, FileMode.Create), System.Text.Encoding.Default))
                {
                    bw.Write(Resource.ProxySwitchConfig);
                    bw.Close();
                }
            }

            //Step: Set System
            RegistryKey key = Registry.CurrentUser.OpenSubKey(REG_KEY, true);

            key.SetValue(REG_VALUE_CONFIGURL, "file://" + configFileName.Replace('\\', '/'), RegistryValueKind.String);

            key.Close();
        }

        private static void notifyIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right)
                return;

            MenuItem mi = new MenuItem { Enabled = false };

            if (controller.Running())
                mi.Text = "当前状态：运行中";
            else
                mi.Text = "当前状态：未运行";

            if (menu.MenuItems.Count == 6)
                menu.MenuItems.RemoveAt(0);

            menu.MenuItems.Add(0, mi);
        }

        private static void menu_RestartClick(object sender, EventArgs e)
        {
            if (controller.Stop())
                controller.Start();
        }

        private static void menu_SettingClick(object sender, EventArgs e)
        {
            FormSetting fs = new FormSetting();
            fs.Show();
        }

        private static void menu_ExitClick(object sender, EventArgs e)
        {
            controller.Stop();

            notifyIcon.Visible = false;

            Application.Exit();
        }
    }
}